package com.Policing.Fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.Policing.Receive.Grant_rfid;
import com.Policing.Since_case.Since_case;
import com.demo.WaveSwipeRefreshLayout;
import com.Policing.Adapter.Public_list_item_Adapter;
import com.Policing.Ip_set.Ip_Set;
import com.Policing.R;
import com.Policing.Receive.Receive_add;
import com.Policing.Receive.Receive_issued;
import com.Policing.bean.receives_bean;
import com.Policing.menu.SlideSideMenuExampleActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lemonade.widgets.slidesidemenu.SlideSideMenuTransitionLayout;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by zq on 2017/1/13.
 */

public class Fragment3 extends Fragment {
	private ImageView imageView;
	private ImageView add;
	private SlideSideMenuExampleActivity mainActivity;
	private ListView lv_list;
	WaveSwipeRefreshLayout mWaveSwipeRefreshLayout;
	private String roleid,id;
	private TextView title_text;
	private ImageView screen;
	private String url="http://"+ Ip_Set.IP+"/api/receivesQuery";
	private static int imageResourceIndex = 0;
	List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();//全部
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_second3, null);
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mainActivity = (SlideSideMenuExampleActivity) getActivity();
		SharedPreferences mm = getActivity().getSharedPreferences("mm", Context.MODE_PRIVATE);//1.通过上下文得到
		 roleid = mm.getString("roleid","");
			id = mm.getString("id","");
		init();
	}
	private void load() {
		new Thread(){
			@Override
			public void run() {
				try {
					OkHttpClient client = new OkHttpClient();
					RequestBody requestBody = new FormBody.Builder()
							.add("id",id)
							.add("source", Since_case.set().getSource())
							.build();
					Request request = new Request.Builder()
							.url(url)
							.post(requestBody).build();
					Response response = client.newCall(request).execute();
					String responseData = response.body().string();
					showResponse(responseData);

				} catch (IOException e) {
					e.printStackTrace();

				}


			}
		}.start();
	}

	private void showResponse(final String responseData) {
		mainActivity.runOnUiThread(() -> {
			list.clear();
			if (responseData.equals("[]")){
				Toast.makeText(mainActivity,"无领用单",Toast.LENGTH_SHORT).show();
				return;
			}

			mWaveSwipeRefreshLayout.setRefreshing(false);//停止加载动画
			Gson gson = new Gson();
			List<receives_bean> userList = gson.fromJson(responseData, new TypeToken<List<receives_bean>>(){}.getType());
			for(int i = 0;i < userList.size();i++){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("odd_tv","领用单号");
				map.put("creator_tv","数量");
				map.put("sum_tv","领用部门");
				map.put("state_tv","领用人");
				map.put("scope_tv","状态");
				map.put("Date_created_tv","申请时间");
				map.put("Inventory_date_tv","领用时间");
				String zt = "";
				if (userList.get(i).getStatus().equals("1")){
					zt = "申请中";
					Since_case.set().setIsAudit("0");//未审核
				}
				if (userList.get(i).getStatus().equals("2")){
					zt = "待发放";
					Since_case.set().setIsAudit("2");//通过
				}
				if (userList.get(i).getStatus().equals("3")){
					zt = "已发放";
					Since_case.set().setIsAudit("2");
				}
				if (userList.get(i).getStatus().equals("0")){
					zt = "驳回";
					Since_case.set().setIsAudit("1");//不通过
				}
				if (userList.get(i).getStatus().equals("4")){
					zt = "部分发放";
					Since_case.set().setIsAudit("2");
				}
				map.put("scope",zt);
				map.put("state",userList.get(i).getReceivesUser());
				map.put("odd",userList.get(i).getReceivesId());
				if (userList.get(i).getReceivesDate().equals("")){
map.put("Inventory_date",userList.get(i).getReceivesDate());
				}else {
map.put("Inventory_date",userList.get(i).getReceivesDate().substring(0,10));
				}
				if (userList.get(i).getReapplyDate().equals("")){
					map.put("Date_created",userList.get(i).getReapplyDate());
				}else {
					map.put("Date_created",userList.get(i).getReapplyDate().substring(0,10));
				}

				map.put("creator",userList.get(i).getSum());
				map.put("sum",userList.get(i).getReceivesDept());
				list.add(map);
Collections.reverse(list);
			}
			//适配器，刚刚重写的！
			Public_list_item_Adapter re_ad = new Public_list_item_Adapter(mainActivity,list);
			lv_list.setAdapter(re_ad);
		});

	}

	private void init() {
		title_text = (TextView) mainActivity.findViewById(R.id.title_text);
		title_text.setText("领用");
		screen = (ImageView) mainActivity.findViewById(R.id.screen);
		lv_list = (ListView) getActivity().findViewById(R.id.lv_Receive);
		add = (ImageView) getActivity().findViewById(R.id.add);
		add.setOnClickListener(v -> {
			Intent intent = new Intent(mainActivity,Receive_add.class);
			startActivity(intent);
		});
		lv_list.setOnItemClickListener((adapterView, view, i, l) -> {
			if (String.valueOf(list.get(i).get("scope")).equals("待发放")||String.valueOf(list.get(i).get("scope")).equals("部分发放")){
				Intent intent = new Intent(mainActivity, Grant_rfid.class);
				intent.putExtra("coding_name", String.valueOf(list.get(i).get("odd")));
				intent.putExtra("status", String.valueOf(list.get(i).get("scope")));
				startActivity(intent);
			}else {
				Intent intent = new Intent(mainActivity, Receive_issued.class);
				intent.putExtra("coding_name", String.valueOf(list.get(i).get("odd")));
				intent.putExtra("status", String.valueOf(list.get(i).get("scope")));
				startActivity(intent);
			}
		});
		imageView = (ImageView) getActivity().findViewById(R.id.opmenu);
		imageView.setOnClickListener(v -> {
			SlideSideMenuTransitionLayout mSlideSideMenu= (SlideSideMenuTransitionLayout)getActivity().findViewById(R.id.slide_side_menu);
			mSlideSideMenu.toggle();
		});
		mWaveSwipeRefreshLayout = (WaveSwipeRefreshLayout) getActivity().findViewById(R.id.main_swipe);
		mWaveSwipeRefreshLayout.setColorSchemeColors(Color.WHITE, Color.WHITE);//设置水滴中旋转动画的颜色，参数1:水滴落下过程中的颜色，参数2:水滴已经落下后的颜色
		mWaveSwipeRefreshLayout.setWaveColor(0xFF3794fc);//设置水滴样色
		//mWaveSwipeRefreshLayout.setMaxDropHeight(1500);//设置最大下拉高度
		mWaveSwipeRefreshLayout.setOnRefreshListener(new WaveSwipeRefreshLayout.OnRefreshListener(){
			@Override
			public void onRefresh() {
				//模拟3秒的加载过程
				new Handler().postDelayed(() -> {
					load();
					//接下来可以刷新ListView
				}, 1200);


			}

		});
	}

	@Override
	public void onStart() {
		Timer timer=new Timer();//实例化Timer类
		timer.schedule(new TimerTask(){
			public void run(){
				load();
				this.cancel();
			}
		},300);//五百毫秒
		super.onStart();
	}
}
